<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\ShanghuPic;
use App\ShanghuType;
use App\Shop;
use App\ShopInfo;
use App\ShopInfos;
use App\Store;
use App\Vip;
use Illuminate\Http\Request;
use DB;

/**
 * Class DiscoveryController
 * @package App\Http\Controllers
 */
class DiscoveryController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 商铺列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function shopList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::ShopList());
        }
        return view('shop.shopList');
    }

    /**
     * 添加商铺
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function shopPublish(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('shop.shopPublish');
        }
        if ($request->lng < 0 || $request->lat < 0 || $request->lng > 180 || $request->lat > 90) {
            return back()->with('status', 2)->with('message', '经纬度不正确');
        }
        //post请求则添加商铺信息
        $return = ShopInfo::ShopPublish($request);
        if ($return['status'] !== 1) {
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        //添加成功重定向到列表页
        return redirect('/discovery/shopList')->with('status', $return['status'])->with('message', $return['message']);
    }


    /**
     * 删除商铺
     * @param $shopInfo
     * @return array
     */
    public function shopDel($shopInfo)
    {
        $del = DB::table('s_shanghu')->where('id', $shopInfo)->delete();
        if (!$del) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }


    /**
     * 修改店铺  old
     * @param Request $request
     * @param $shopInfo
     * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function shopModifyd(Request $request, $shopInfo)
    {
        $shopInfo = ShopInfo::find($shopInfo);
        if (!$shopInfo) {
            return redirect(url('/discovery/shopList'));
        }

        if ($request->isMethod('post')) {
            if ($request->lng < 0 || $request->lat < 0 || $request->lng > 180 || $request->lat > 90) {
                return back()->with('status', 2)->with('message', '经纬度不正确');
            }
            $return = ShopInfo::ShopModify($request, $shopInfo);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        $pic = DB::table('s_shanghu_pic')->select('id', 'pic')->where('shanghu_id', $shopInfo->id)->get();
        $pics = [];
        $i = 1;
        foreach ($pic as $item) {
            $pics[$i]['id'] = $item->id;
            $pics[$i]['pic'] = $item->pic;
            $i++;
        }
        $phones = DB::table('s_shanghu_phone')->select('phone_num')->where('shanghu_id', $shopInfo->id)->get();
        $phone = '';
        foreach ($phones as $item) {
            $phone .= $item->phone_num . ' ';
        }
        return view('shop.shopModify')->with(compact('shopInfo', 'pics', 'phone'));
    }


    /**
     * @param ShopInfo $shopInfo
     * @return $this
     */
    public function shopModify(ShopInfo $shopInfo)
    {
        $shop = $shopInfo;
        $huati_id = explode(',', $shop->huati_id);
        //图片
        $pics = ShanghuPic::select('pic')->where('shanghu_id', $shop->id)->orderByDesc('cover')->get();

        //是否有待审核的信息
        if ($shop->status === ShopInfo::STATUS_NEW) {
            $shopinfo = ShopInfos::select('piclist', 'phone', 'withdrawtype', 'alipay', 'business_time1', 'business_time2', 'introduce')
                ->where('id', $shop->shinfoid)
                ->where('status', ShopInfos::STATUS_WAITING)
                ->first();
        } else {
            $shopinfo = null;
        }
        $phones = DB::table('s_shanghu_phone')->select('phone_num')->where('shanghu_id', $shopInfo->id)->get();
        $phone = '';
        foreach ($phones as $item) {
            $phone .= $item->phone_num . ' ';
        }
        return view('store.shopInfo')->with(compact('shop', 'shopinfo', 'huati_id', 'pics', 'phone'));
    }

    /**
     * 修改信息
     * @param Request $request
     * @param ShopInfo $shopInfo
     * @return \Illuminate\Http\JsonResponse
     */
    public function vipModify(Request $request, ShopInfo $shopInfo)
    {
        //圈子校验
        if ($request->huati_id) {
            if (!is_array($request->huati_id)) {
                return response()->json(error('圈子权限有误'));
            }
            if (count($request->huati_id) !== DB::table('tbl_huati')->whereIn('id', $request->huati_id)->count()) {
                return response()->json(error('圈子权限有误'));
            }
            $huati_id = join(',', $request->huati_id);
        } else {
            $huati_id = $request->huati_id;
        }

        return response()->json(ShopInfo::ShopVipModify($shopInfo, $request, $huati_id));
    }


    /**
     * 通过审核
     * @param ShopInfo $shopInfo
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function statusOn(ShopInfo $shopInfo)
    {
        return response()->json(ShopInfo::Status($shopInfo, ShopInfo::STATUS_YES));
    }


    /**
     * 不通过审核
     * @param ShopInfo $shopInfo
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function statusOff(ShopInfo $shopInfo)
    {
        return response()->json(ShopInfo::Status($shopInfo, ShopInfo::STATUS_REFUSE));

    }

    /**
     * 拒绝新信息修改
     * @param ShopInfo $shopInfo
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function newStatusOff(ShopInfo $shopInfo)
    {
        return response()->json(ShopInfo::NewInfoOff($shopInfo));
    }


    /**
     * 同意新信息修改
     * @param ShopInfo $shopInfo
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function newStatusOn(ShopInfo $shopInfo)
    {
        return response()->json(ShopInfo::NewInfoOn($shopInfo));
    }

    /**
     * 删除店铺展示图
     * @param $pic_id
     * @return array
     */
    public function shopPicDel($pic_id)
    {
        if (!DB::table('s_shanghu_pic')->where('id', $pic_id)->update(['pic' => NULL])) {
            return ['status' => 2, 'message' => '删除失败'];
        }

        return ['status' => 1, 'message' => '删除成功'];
    }


    /**
     * 商铺类型列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function shopTypeList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::ShopTypeList());
        }
        return view('shop.shopTypeList');
    }


    /**
     * 删除商铺类型
     * @param $shanghuType
     * @return array
     * @throws \Exception
     */
    public function shopTypeDel($shanghuType)
    {
        if (!DB::table('s_shanghu_type')->where('id', $shanghuType)->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }

        return ['status' => 1, 'message' => '删除成功'];
    }


    /**
     * 添加类型
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function shopTypePublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = ShanghuType::ShopTypePublish($request);
            if ($return['status'] !== 1) {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }
            //添加成功重定向到列表页
            return redirect('/discovery/shopTypeList')->with('status', $return['status'])->with('message', $return['message']);
        }

        return view('shop.shopTypePublish');
    }


    /**
     * 修改类型
     * @param Request $request
     * @param $shanghuType
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function shopTypeModify(Request $request, $shanghuType)
    {
        if ($request->isMethod('post')) {
            $return = ShanghuType::ShopTypeModify($request, $shanghuType);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }

        $type = ShanghuType::select('id', 'store_type', 'weight', 'pic', 'act', 'pin_pic')->findOrFail($shanghuType);
        return view('shop.shopTypeModify')->with(compact('type'));
    }



    /**
     * 普通商户权限设置
     * @param Request $request
     * @return $this
     */
    public function storeConfig(Request $request)
    {
        if ($request->isMethod('post')) {
            return Store::ModifyStoreConfig($request);
        }
        return view('store.config')->with('config', Store::select('title', 'circle', 'docnum', 'shownum', 'coupon', 'keeptime', 'scanuser', 'qunsend')->first());
    }



    /**
     * VIP级别列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function storeVipList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(DataTables::StoreVipList());
        }

        return view("store.storeVipList");
    }

    /**
     * 添加VIP级别
     * @return array
     */
    public function storeVipPublish()
    {
        return Vip::StoreVipPublish();
    }

    /**
     * VIP级别信息
     * @param Vip $vip
     * @return array
     */
    public function storeVipInfo(Vip $vip)
    {
        $return = [];
        foreach (['id', 'weight', 'title', 'timelimt', 'circle', 'docnum', 'shownum', 'scanuser', 'coupon', 'consulttype', 'keeptime', 'status', 'qunsend'] as $item) {
            $return[$item] = $vip->$item;
        }
        return $return;
    }

    /**
     * 修改VIP级别
     * @param Vip $vip
     * @return array
     */
    public function storeVipModify(Vip $vip)
    {
        return Vip::StoreVipModify($vip);

    }

}
